package edu.ptu.shortlink.project.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import edu.ptu.shortlink.project.dao.entity.ShortLinkDO;
import edu.ptu.shortlink.project.dao.mapper.ShortLinkMapper;
import edu.ptu.shortlink.project.dto.req.RecycleBinPageReqDTO;
import edu.ptu.shortlink.project.dto.req.RecycleBinRecoverReqDTO;
import edu.ptu.shortlink.project.dto.req.RecycleBinRemoveReqDTO;
import edu.ptu.shortlink.project.dto.req.RecycleBinSaveReqDTO;
import edu.ptu.shortlink.project.dto.resp.ShortLinkPageRespDTO;
import edu.ptu.shortlink.project.service.IRecycleBinService;
import edu.ptu.shortlink.project.toolkit.LinkUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.concurrent.TimeUnit;

import static edu.ptu.shortlink.project.common.constant.RedisKeyConstant.GOTO_SHORT_LINK_KEY;
import static edu.ptu.shortlink.project.common.constant.RedisKeyConstant.SHORT_LINK_IS_NULL_KEY;

/**
 * @BelongsProject: shortlink
 * @BelongsPackage: edu.ptu.shortlink.project.service.impl
 * @Author: HYJ
 * @CreateTime: 2025-05-01  21:32
 * @Description: 回收站服务实现类
 * @Version: 1.0
 */
@Service
@RequiredArgsConstructor
public class RecycleBinServiceImpl extends ServiceImpl<ShortLinkMapper, ShortLinkDO> implements IRecycleBinService {

    private final StringRedisTemplate stringRedisTemplate;

    @Override
    public void saveToRecycleBin(RecycleBinSaveReqDTO requestParam) {
        LambdaUpdateWrapper<ShortLinkDO> updateWrapper = Wrappers.lambdaUpdate(ShortLinkDO.class)
                .eq(ShortLinkDO::getGid, requestParam.getGid())
                .eq(ShortLinkDO::getFullShortUrl, requestParam.getFullShortUrl())
                .eq(ShortLinkDO::getEnableStatus, 0);

        ShortLinkDO shortLinkDO = ShortLinkDO.builder()
                .enableStatus(1)
                .build();

        baseMapper.update(shortLinkDO, updateWrapper);
        // 删除跳转缓存
        stringRedisTemplate.delete(String.format(GOTO_SHORT_LINK_KEY, requestParam.getFullShortUrl()));
    }

    @Override
    public IPage<ShortLinkPageRespDTO> pageRecycleBin(RecycleBinPageReqDTO requestParam) {
        LambdaQueryWrapper<ShortLinkDO> queryWrapper = Wrappers.lambdaQuery(ShortLinkDO.class)
                .in(ShortLinkDO::getGid, requestParam.getGidList())
                .eq(ShortLinkDO::getEnableStatus, 1)
                .orderByDesc(ShortLinkDO::getUpdateTime);

        IPage<ShortLinkDO> resultPage = baseMapper.selectPage(requestParam, queryWrapper);

        return resultPage.convert(
                each -> {
                    ShortLinkPageRespDTO result = BeanUtil.toBean(each, ShortLinkPageRespDTO.class);
                    result.setDomain("http://" + each.getDomain());
                    return result;
                }
        );
    }

    @Override
    public void recoverFromRecycleBin(RecycleBinRecoverReqDTO requestParam) {
        LambdaUpdateWrapper<ShortLinkDO> updateWrapper = Wrappers.lambdaUpdate(ShortLinkDO.class)
                .eq(ShortLinkDO::getGid, requestParam.getGid())
                .eq(ShortLinkDO::getFullShortUrl, requestParam.getFullShortUrl());

        ShortLinkDO shortLinkDO = ShortLinkDO.builder()
                .enableStatus(1)
                .build();
        baseMapper.update(shortLinkDO, updateWrapper);
        //缓存预热
        stringRedisTemplate.opsForValue().set(
                String.format(GOTO_SHORT_LINK_KEY, requestParam.getFullShortUrl()),
                requestParam.getFullShortUrl(),
                LinkUtil.getShortLintCacheValidDate(shortLinkDO.getValidDate()),
                TimeUnit.MILLISECONDS
        );
        // 删除跳转缓存
        stringRedisTemplate.delete(String.format(SHORT_LINK_IS_NULL_KEY, requestParam.getFullShortUrl()));
    }

    @Override
    public void removeFromRecycleBin(RecycleBinRemoveReqDTO requestParam) {
        LambdaUpdateWrapper<ShortLinkDO> updateWrapper = Wrappers.lambdaUpdate(ShortLinkDO.class)
                .eq(ShortLinkDO::getGid, requestParam.getGid())
                .eq(ShortLinkDO::getEnableStatus, 1)
                .eq(ShortLinkDO::getFullShortUrl, requestParam.getFullShortUrl())
                .set(ShortLinkDO::getDelTime, System.currentTimeMillis());

        baseMapper.delete(updateWrapper);
    }
}
